package tk.xboot.ws;

import javax.websocket.ClientEndpoint;
import javax.websocket.ContainerProvider;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import java.net.URI;

@ClientEndpoint
public class WebSocketCli {
    private String deviceId;
    private Session session;

    public WebSocketCli(){

    }

    public WebSocketCli(String deviceId) {
        this.deviceId = deviceId;
    }


    protected boolean start(){
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        //String uri = "ws://ws://192.168.12.161/orderapp/recommend?role=1&group=recommend&dldm="+ deviceId;
        //String uri = "ws://ws://192.168.0.29/ws/"+ deviceId;
        String uri = "ws://192.168.0.22:1903/ws/"+ deviceId;
        System.out.println("Connecting to " + uri);
        try {
            session = container.connectToServer(WebSocketCli.class, URI.create(uri));
            System.out.println("count:" + deviceId);

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return  true;
    }


    public static void main(String[] args) {
        for (int i = 0; i < 500; i++) {
            WebSocketCli cli = new WebSocketCli(String.valueOf(i));
            if (!cli.start()) {
                System.out.println("Test over!");
                break;
            }
        }
    }
}
